//Audio playback component, only supports WAV files.
import { alsaplayClass } from './libvbar-m-dxalsaplay.so'
import dxCommon from './dxCommon.js'
const alsaplayObj = new alsaplayClass();
const alsaplay = {}

/**
 * alsaplay initialization
 * @param {string} id Handle ID, optional (a unique ID is required if initializing multiple instances).
 * @param {number} volume Volume (optional)
 * @param {number} card Not required
 * @param {number} device Not required
 * @param {number} mixer_ctl_num Not required
 * @returns handle id
 */
alsaplay.init = function (id, volume, card, device, mixer_ctl_num) {
	if (volume === undefined || volume === null) {
		volume = 30
	}
	if (card === undefined || card === null) {
		card = 0
	}
	if (device === undefined || device === null) {
		device = 0
	}
	if (mixer_ctl_num === undefined || mixer_ctl_num === null) {
		mixer_ctl_num = 3
	}
	let pointer = alsaplayObj.alsaplayInit(volume, card, device, mixer_ctl_num)
	if (pointer === undefined || pointer === null) {
		throw new Error("alsaplay.init: init failed")
	}

	return dxCommon.handleId("alsaplay", id, pointer)
}

/**
 * alsaplay Deinitialize
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsaplay.deinit = function (id) {
	let pointer = dxCommon.handleId("alsaplay", id)
	return alsaplayObj.alsaplayDeinit(pointer)
}

/**
 * Play music files
 * @param {string} path The absolute path to the WAV file, starting with '/app/code/', is required. It's typically placed in the project's resource directory (at the same level as src).
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsaplay.play = function (path, id) {
	if (!path) {
		throw new Error("alsaplay.play: 'path' parameter should not be null")
	}
	let pointer = dxCommon.handleId("alsaplay", id)
	return alsaplayObj.alsaplayWav(pointer, path)
}

/**
 * Interrupt currently playing audio
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).）
 * @returns true/false
 */
alsaplay.interrupt = function (id) {
	let pointer = dxCommon.handleId("alsaplay", id)
	return alsaplayObj.alsaplayPlayingInterrupt(pointer)
}

/**
 * Clear playback cache
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns true/false
 */
alsaplay.clearPlayCache = function (id) {
	let pointer = dxCommon.handleId("alsaplay", id)
	return alsaplayObj.alsaplayClearPlayCache(pointer)
}

/**
 * Get volume
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns Returns a numeric volume value, which will not exceed the volume range.
 */
alsaplay.getVolume = function (id) {
	let pointer = dxCommon.handleId("alsaplay", id)
	return alsaplayObj.alsaplayGetVolume(pointer)
}

/**
 * Setting the volume too high or too low will default to the maximum or minimum volume range.
 * @param {number} volume Required
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns 
 */
alsaplay.setVolume = function (volume, id) {
	let pointer = dxCommon.handleId("alsaplay", id)
	return alsaplayObj.alsaplaySetVolume(pointer, volume)
}
/**
 * Get volume range
 * @param {string} id Handle ID, optional (must be consistent with the ID in init).
 * @returns Format {"min":0,"max":6}
 */
alsaplay.getVolumeRange = function (id) {
	let pointer = dxCommon.handleId("alsaplay", id)
	return alsaplayObj.alsaplayGetVolumeRange(pointer)
}

export default alsaplay;
